<b>Import</b><br /><hr />

<table>
	<tr>
		<th>1st Step</th>
		<td>
			Change the '.xls' or '.xlsx' file to a '.cvs' file or '.cvs' files.<br />
			The '.cvs' must be a 'comma delimited' and 'UTF-8' type. 
		</td>
	</tr>
	<tr>
		<th>2nd Step</th>
		<td>
			Separate the '.cvs' or the '.cvs' files into each table.<br />
			Normally, a list file from Ho needs to be separated into artist, album and music files.
		</td>
	</tr>
	<tr>
		<th>3rd Step</th>
		<td>Upload the files to 'JC_80_file_temporary' folder.</td>
	</tr>
	<tr>
		<th>4th Step</th>
		<td>Use below form one by one.</td>
	</tr>
</table><hr />



<form id="JC_import" method="post">

	<table>

		<tr>
			<th>File</th>
			<th>Charset</th>
			<th>Start</th>
			<th>Table</th>
			<th>Field</th>
		</tr>

		<tr>
			<td>

				<select id="JC_import_file" name="JC_import_file">

					<option></option>

					<? foreach( JC_FT( ) as $key => $value ) { ?>

						<? if( strpos( $key, ".csv" ) === false ) continue; ?>

						<option <?= $_REQUEST[ "JC_import_file" ] == $key ? "selected" : "" ?>><?= $key ?></option>

					<? } ?>

				</select>

			</td>
			<td>

				<select name="JC_import_charset">

					<option></option>
					<option <?= $_REQUEST[ "JC_import_charset" ] == "euc-kr" ? "selected" : "" ?>>euc-kr</option>

				</select>

			</td>
			<td>

				<select name="JC_import_start" class="right">

					<? for( $i = 0; $i < 12; $i ++ ) { ?>

						<option <?= intval( $_REQUEST[ "JC_import_start" ] ) == $i + 1 ? "selected" : "" ?>><?= $i + 1 ?></option>

					<? } ?>

				</select>

			</td>
			<td>

				<select id="JC_import_table" name="JC_import_table">

					<option></option>

					<? foreach( $_SESSION[ "JC_scheme" ] as $key => $value) { ?>

						<option <?= $_REQUEST[ "JC_import_table" ] == $key ? "selected" : "" ?>><?= $key ?></option>

					<? } ?>

				</select>

			</td>
			<td><a href="#" class="form" onclick="location.replace( '?JC_file=<?= $_REQUEST[ "JC_file" ] ?>&JC_import_table=' + document.getElementById( 'JC_import_table' ).value );">Field</a></td>
		</tr>
	</table><hr />



	<? if( $_REQUEST[ "JC_import_table" ] ) { ?>

		<table>
			<tr>
				<th>Column</th>
				<th>Field</th>
				<th>FKT</th>
				<th>FKF</th>
				<th>I18N</th>
				<th>Import</th>
			</tr>

			<? for( $i = 0; $i < 26; $i ++ ) { ?>

				<tr>
					<td class="form"><?= chr( $i + 65 ) ?></td>
					<td>

						<select name="JC_import_field_<?= $i ?>">

							<option></option>

							<? foreach( $_SESSION[ "JC_scheme" ][ $_REQUEST[ "JC_import_table" ] ] as $key => $value) { ?>

								<? if( $key == "comment" ) continue; ?>

								<option <?= $_REQUEST[ "JC_import_field_" . $i ] == $key ? "selected" : "" ?>><?= $key ?></option>

							<? } ?>

						</select>

					</td>
					<td>

						<select id="JC_import_FKT_<?= $i ?>" name="JC_import_FKT_<?= $i ?>">

							<option></option>

							<? foreach( $_SESSION[ "JC_scheme" ] as $key => $value) { ?>

								<option <?= $_REQUEST[ "JC_import_FKT_" . $i ] == $key ? "selected" : "" ?>><?= $key ?></option>

							<? } ?>

						</select>

					</td>
					<td>

						<select id="JC_import_FKF_<?= $i ?>" name="JC_import_FKF_<?= $i ?>">

							<option></option>
							<option <?= $_REQUEST[ "JC_import_FKF_" . $i ] == "key_bugs" ? "selected" : "" ?>>key_bugs</option>

						</select>

					</td>
					<td>

						<select name="JC_import_I18N_<?= $i ?>">

							<option></option>
							<option <?= $_REQUEST[ "JC_import_I18N_" . $i ] == "VN" ? "selected" : "" ?>>VN</option>
							<option <?= $_REQUEST[ "JC_import_I18N_" . $i ] == "EN" ? "selected" : "" ?>>EN</option>
							<option <?= $_REQUEST[ "JC_import_I18N_" . $i ] == "KR" ? "selected" : "" ?>>KR</option>

						</select>

					</td>
					<td><? if( $i == 25 ) { ?><a href="#" class="form" onclick="if( confirm( 'Import?' ) ) document.getElementById( 'JC_import' ).submit( );">Import</a><? } ?></td>
				</tr>

			<? } ?>

		</table><hr />

	<? } ?>



</form>



<? if( $_REQUEST[ "JC_import_file" ] ) { ?>

	<table>
		<tr>
			<th>Query</th>
		</tr>
		<tr>
			<td>

				<?

					if( ( $handle = fopen( $JC_path . "/JC_80_file_temporary/" . $_REQUEST[ "JC_import_file" ], "r" ) ) !== false ) {

						while( ( $data = fgetcsv( $handle, 1024, "," ) ) !== false ) {

							$line ++;

							if( $line < intval( $_REQUEST[ "JC_import_start" ] ) ) continue;

							$query = "INSERT INTO `" . $_REQUEST[ "JC_import_table" ] . "` ( ";

							for( $i = 0; $i < 26; $i ++ ) if( $_REQUEST[ "JC_import_field_" . $i ] ) $query .= "`" . $_REQUEST[ "JC_import_field_" . $i ] . "`, ";

							$query .= " `insert` ) VALUES ( ";

							for( $i = 0; $i < 26; $i ++ ) if( $_REQUEST[ "JC_import_field_" . $i ] ) {

								if( $_REQUEST[ "JC_import_FKT_" . $i ] && $_REQUEST[ "JC_import_FKF_" . $i ] ) {
									
									$query .= "( SELECT `key` FROM `" . $_REQUEST[ "JC_import_FKT_" . $i ] . "` WHERE `" . $_REQUEST[ "JC_import_FKF_" . $i ] . "` = '" . ( $_REQUEST[ "JC_import_charset" ] ? iconv( $_REQUEST[ "JC_import_charset" ], "utf-8", $data[ $i ] ) : $data[ $i ] ) .  "' ), ";

								} else {

									if( empty( $data[ $i ] ) && $_SESSION[ "JC_scheme" ][ $_REQUEST[ "JC_import_table" ] ][ "JC_import_field_" . $i ][ "null" ] ) {

										$query .= "NULL, ";

									} else {

										$query .= "'";

										if( $_REQUEST[ "JC_import_I18N_" . $i ] ) $query .= $_REQUEST[ "JC_import_I18N_" . $i ] . ": ";

										$query .= trim( ( $_REQUEST[ "JC_import_charset" ] ? iconv( $_REQUEST[ "JC_import_charset" ], "utf-8", $data[ $i ] ) : $data[ $i ] ) );

										if( $_REQUEST[ "JC_import_I18N_" . $i ] ) $query .= "\r\n";

										$query .= "', ";

									}

								}

							}

							$query .= " NOW( ) )";

							JC_query( $query );

							echo $query . "<br />";

							flush( );

						}

						fclose( $handle );

					}

				?>

				The last query has been executed.

			</td>
		</tr>
	</table>

<? } ?>
